package com.lovo.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.project.bean.ClientBean;
import com.lovo.project.bean.SupplierBean;
import com.lovo.project.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/client")
public class ClientController {

    @Autowired
    ClientService clientService;

    @RequestMapping("/queryClientList")
    public Page<ClientBean> queryClientList(String clientName,String clientType,Integer pg,Integer pgsize){
        QueryWrapper<ClientBean> queryWrapper=new QueryWrapper<>();
        if (clientName!=null && clientName !=""){
            queryWrapper.like("client_name",clientName);
        }
        if (clientType!=null && clientType!=""){
            queryWrapper.like("client_type",clientType);
        }
        Page<ClientBean> page=new Page<>(pg,pgsize);
        Page<ClientBean> pageInfo=clientService.queryClientList(page,queryWrapper);
        return pageInfo;
    }

    @RequestMapping("/queryClient")
    public ClientBean queryClient(Integer clientId){
        ClientBean clientBean=clientService.queryClient(clientId);
        return clientBean;
    }

    @RequestMapping("/updateClient")
    public String updateClient(Integer clientId,String clientDuty,String clientTel){
        ClientBean clientBean=new ClientBean();
        clientBean.setClientId(clientId);
        clientBean.setClientDuty(clientDuty);
        clientBean.setClientTel(clientTel);
        clientService.updateClient(clientBean);
        return "ok";
    }

    @RequestMapping("/addClient")
    public String addClient(String clientName,String clientDuty,String clientTel,String clientAddress,String clientType,String clientEmail){
        ClientBean clientBean=new ClientBean();
        clientBean.setClientName(clientName);
        clientBean.setClientDuty(clientDuty);
        clientBean.setClientTel(clientTel);
        clientBean.setClientAddress(clientAddress);
        clientBean.setClientEmail(clientEmail);
        clientBean.setClientType(clientType);
        clientService.insertClient(clientBean);
        return "ok";
    }


    @RequestMapping("/queryClientNameList")
    public List<String> queryClientNameList(){
        List<ClientBean> list =clientService.queryClientNameList(null);
        List<String> list1=new ArrayList<>();
        for(ClientBean b:list){
            list1.add(b.getClientName());
        }
        return list1;
    }
}
